package com.shop.shopserver.controller.user;

import com.shop.shopcommon.result.Result;
import com.shop.shoppojo.dto.CartDTO;
import com.shop.shoppojo.dto.CartDeleteDTO;
import com.shop.shoppojo.entity.Cart;
import com.shop.shoppojo.vo.CartInsertVO;
import com.shop.shoppojo.vo.CartListVO;
import com.shop.shopserver.service.impl.CartServiceImpl;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("cart")
@Slf4j
public class CartController {
    @Autowired
    private CartServiceImpl cartServiceImpl;

    /**
     * 添加购物车
     * @param cartDTO
     * @return
     * @throws IOException
     */

    @PostMapping("add")
    @ApiOperation("添加购物车")
    public Result<CartInsertVO> add(@RequestBody CartDTO cartDTO) throws IOException {
        log.info("添加购物车：{}", cartDTO);
        return cartServiceImpl.add(cartDTO);
    }

    /**
     * 更新购物车
     * @param cartDTO
     * @return
     * @throws IOException
     */

    @PostMapping("update")
    @ApiOperation("更新购物车")
    public Result<CartInsertVO> updateCartGoodsNum(@RequestBody CartDTO cartDTO) throws IOException {
        log.info("更新购物车：{}", cartDTO);
        return cartServiceImpl.updateCartGoodsNum(cartDTO);
    }


    /**
     * 删除购物车
     * @param cartDeleteDTO
     * @return
     * @throws IOException
     */

    @PostMapping("delete")
    @ApiOperation("删除购物车")
    public Result<Cart> deleteCart(@RequestBody CartDeleteDTO cartDeleteDTO) throws IOException {
        log.info("删除购物车：{}", cartDeleteDTO);
        return cartServiceImpl.deleteCart(cartDeleteDTO);
    }


    /**
     * 获取购物车列表
     * @param cartDTO
     * @return
     * @throws IOException
     */

    @GetMapping("getCartList")
    @ApiOperation("获取购物车列表")
    public Result<CartListVO> getCartList(CartDTO cartDTO) throws IOException {
        log.info("获取购物车列表");
        return cartServiceImpl.getCartList(cartDTO);
    }
}
